package com.myway.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myway.dto.common.JsonData;
import com.myway.dao.TabTravelDateMapper;
import com.myway.dao.TabTravelMapper;
import com.myway.dao.TabTravelOrderMapper;
import com.myway.dao.TabTravlePeoMapper;
import com.myway.dto.param.TravelParam;
import com.myway.pojo.*;
import com.myway.service.TravelService;
import com.myway.utils.DataGridResult;
import com.myway.dto.util.DateUtils;
import com.myway.dto.util.OrderNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class TravelServiceImpl implements TravelService{

    /**
     * 获取热门线路集合
     */
    @Autowired
    private TabTravelMapper tabTravelMapper;

    @Autowired
    private TabTravelDateMapper tabTravelDateMapper;

    @Autowired
    private TabTravelOrderMapper tabTravelOrderMapper;

    @Autowired
    private TabTravlePeoMapper tabTravlePeoMapper;

    @Override
    public JsonData findTravelLoad() {
        List<TabTravel> tabTravels = tabTravelMapper.selectByHotLoad();
        if(tabTravels.size()>0){
            return JsonData.success(tabTravels);
        }
        return JsonData.fail("暂无数据");
    }

    @Override
    public TabTravelWithBLOBs getOneTravelLoad(Integer id) {
        TabTravelWithBLOBs bloBs = tabTravelMapper.selectByPrimaryKey(id);
        return bloBs;
    }

    @Override
    public JsonData getLoadsTravels(TravelParam travelParam){
        PageHelper.startPage(travelParam.getPageIndex()+1,travelParam.getPageSize());
        List<TabTravel> tabTravels = tabTravelMapper.selectByManyInf(travelParam);
        DataGridResult result = new DataGridResult();
        if(tabTravels.size()>0){
            result.setRows(tabTravels);
            PageInfo<TabTravel>  pageInfo = new PageInfo<TabTravel>(tabTravels);
            result.setTotal(pageInfo.getTotal());
            return  JsonData.success(result);
        }
        return  JsonData.success(result);
    }

    @Override
    public JsonData getOneLoadsDetails(TabTravelDate tabTravelDate, Integer traNum) {
        TabTravelDate tabTravel=null;
        do {
            tabTravel = tabTravelDateMapper.selectByTravelId(tabTravelDate);
            if(tabTravel ==null){
                break;
            }
            tabTravelDate.setTravleDate(DateUtils.addOneDay(tabTravelDate.getTravleDate()));
        }while(tabTravel.getNum()> traNum);
        return JsonData.success(tabTravel !=null?tabTravel.getTravleDate():tabTravelDate.getTravleDate());
    }

    /**
     * 根据线路总票数，以及预定票数，本次预定人数，计算余票数
     * @param tabTravelDate
     * @param traNum
     * @param perNum
     * @return
     */
    @Override
    public JsonData getOneLoadsTickets(TabTravelDate tabTravelDate, Integer traNum, Integer perNum) {
        TabTravelDate tabTravelDate1 = tabTravelDateMapper.selectByTravelId(tabTravelDate);
        int num =0;
        if(tabTravelDate1 !=null){
            num =traNum-perNum-tabTravelDate1.getNum();
        }else{
            num =traNum-perNum;
        }
        return JsonData.success(num);
    }

    /**
     * 生成旅游线路订单
     * @param travelOrder
     * @return
     */
    @Override
    public JsonData getTravelPeos(TabTravelOrder travelOrder){
        travelOrder.setId(OrderNumberGenerator.getOrderNo());
        travelOrder.setStatusT(1);
        travelOrder.setSetTime(new Date());
        travelOrder.setUpdateDate(new Date());
        int i = tabTravelOrderMapper.insertSelective(travelOrder);
        if(i>0){
            List<TabTravlePeo> list = JSONObject.parseArray(travelOrder.getData(), TabTravlePeo.class);
            Iterator<TabTravlePeo> iterator = list.iterator();
            while (iterator.hasNext()){
                TabTravlePeo next = iterator.next();
                next.setSetTime(new Date());
                next.setUpdateTime(new Date());
                next.setOrderId(travelOrder.getId());
                tabTravlePeoMapper.insert(next);
            }
            return JsonData.success(travelOrder.getId());
        }else{
            return JsonData.fail("500");
        }

    }
}
